package com.cn.dream_order_server.controller;

import com.cn.dream_order_server.async.DeliverGoodsAsync;
import com.cn.dream_order_server.pojo.OrderLogisticstype;
import com.cn.dream_order_server.pojo.OrderOrder;
import com.cn.dream_order_server.pojo.OrderOrdertypeManage;
import com.cn.dream_order_server.service.OrderLogisticstypeService;
import com.cn.dream_order_server.service.OrderOrderBackstageService;
import com.cn.dream_order_server.service.OrderOrdertypeManageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;


@Controller
public class OrderOrderBackstageController {

    @Autowired
    private OrderOrderBackstageService orderOrderBackstageService;

    @Autowired
    private OrderLogisticstypeService orderLogisticstypeService;

    @Autowired
    private DeliverGoodsAsync deliverGoodsAsync;

    @Autowired
    private OrderOrdertypeManageService orderOrdertypeManageService;

    //展示全部页
    @RequestMapping("/wlcGetOrderForm")
    public String wlcGetOrderForm(Integer pageNo,Integer pageSize,Map map,String orderId,String placeTime){
        //判断pageNo是否为空
        if(pageNo == null || pageNo < 1 ){
            pageNo = 1;
        }
        //判断pageSize是否为空
        if(pageSize == null ){
            pageSize = 10;
        }
        //初始化pageHelper
        PageHelper.startPage(pageNo,pageSize);
        //查询订单信息
        List<OrderOrder> list = orderOrderBackstageService.getOrderOrderList(orderId,placeTime);
        //创建pageinfo对象
        PageInfo<OrderOrder> pageInfo = new PageInfo<OrderOrder>(list);
        //查询全部快递信息
        List<OrderLogisticstype> logisticstypeList = orderLogisticstypeService.getOrderLogisticstypeAllList();
        //判断总页数是否小于1
        if(pageInfo.getPages()<1){
            pageInfo.setPages(1);
        }
        //查询所有条数
        Long zLong = orderOrderBackstageService.countAllOrderOrderNum();

        //查询所有的状态
        List<OrderOrdertypeManage> ordertypeManageList = orderOrdertypeManageService.getOrderOrdertypeManageAllList();
        map.put("ordertypeManageList",ordertypeManageList);
        //将pageinfo对象存到map传递到前端
        map.put("pageInfo",pageInfo);
        //将快递信息存到map传递到前端
        map.put("logisticstypeList",logisticstypeList);
        //将条件存到map传递到前端
        map.put("orderId",orderId);
        //将条件存到map传递到前端
        map.put("placeTime",placeTime);
        map.put("zLong",zLong);
        return "Order_handling";
    }

    //展示全部页刷新
    @RequestMapping("/wlcGetOrderFormLoad")
    public String wlcGetOrderFormLoad(Integer pageNo,Integer pageSize,Map map,String orderId,String placeTime){
        //判断pageNo是否为空
        if(pageNo == null || pageNo < 1 ){
            pageNo = 1;
        }
        //判断pageSize是否为空
        if(pageSize == null ){
            pageSize = 10;
        }
        //初始化pageHelper
        PageHelper.startPage(pageNo,pageSize);
        //查询订单信息
        List<OrderOrder> list = orderOrderBackstageService.getOrderOrderList(orderId,placeTime);
        //创建pageinfo对象
        PageInfo<OrderOrder> pageInfo = new PageInfo<OrderOrder>(list);
        //查询全部快递信息
        List<OrderLogisticstype> logisticstypeList = orderLogisticstypeService.getOrderLogisticstypeAllList();
        //将pageinfo对象存到map传递到前端
        map.put("pageInfo",pageInfo);
        //将快递信息存到map传递到前端
        map.put("logisticstypeList",logisticstypeList);
        //将条件存到map传递到前端
        map.put("orderId",orderId);
        //将条件存到map传递到前端
        map.put("placeTime",placeTime);
        return "Order_handling::div1";
    }

    //发货
    @RequestMapping("/wlcDoDeliverGoods")
    @ResponseBody
    public Object wlcDoDeliverGoods(String orderId,String logisticsId,String logisticstypeId){
        //异步执行添加物流信息
        Future<Integer> i = deliverGoodsAsync.deliverGoods(logisticsId, logisticstypeId);
        //根据订单id获取订单信息
        OrderOrder orderOrder = orderOrderBackstageService.getOrderOrderByOrderId(orderId);
        //定义布尔值  用于返回值
        Boolean flag = false;
        if(orderOrder!=null){
            try {
                //判断物流信息是否添加成功
                if (i.get() == 1){
                    orderOrder.setOrderOther1(logisticsId);
                    orderOrder.setOrderOther4(4);
                    //调用修改方法 返回一个布尔值
                    flag = orderOrderBackstageService.updateOrderOrderByOrderId(orderOrder);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

        }
        return flag;
    }


    //展示已完成页
    @RequestMapping("/wlcGetOrderForm_off_the_stocks")
    public String wlcGetOrderForm_off_the_stocks(Integer pageNo,Integer pageSize,Map map,String orderId,String placeTime,Integer orderOther4){
        //判断pageNo是否为空
        if(pageNo == null || pageNo < 1 ){
            pageNo = 1;
        }
        //判断pageSize是否为空
        if(pageSize == null ){
            pageSize = 10;
        }
        //初始化pageHelper
        PageHelper.startPage(pageNo,pageSize);
        //查询订单信息
        List<OrderOrder> list = orderOrderBackstageService.getOrderOrderListByOrderOther4(orderId,placeTime,orderOther4);
        //创建pageinfo对象
        PageInfo<OrderOrder> pageInfo = new PageInfo<OrderOrder>(list);
        //查询全部快递信息
        List<OrderLogisticstype> logisticstypeList = orderLogisticstypeService.getOrderLogisticstypeAllList();
        //判断总页数是否小于1
        if(pageInfo.getPages()<1){
            pageInfo.setPages(1);
        }
        //查询所有条数
        Long zLong = orderOrderBackstageService.countAllOrderOrderNum();

        //查询所有的状态
        List<OrderOrdertypeManage> ordertypeManageList = orderOrdertypeManageService.getOrderOrdertypeManageAllList();
        map.put("ordertypeManageList",ordertypeManageList);
        //将pageinfo对象存到map传递到前端
        map.put("pageInfo",pageInfo);
        //将快递信息存到map传递到前端
        map.put("logisticstypeList",logisticstypeList);
        //将条件存到map传递到前端
        map.put("orderId",orderId);
        //将条件存到map传递到前端
        map.put("placeTime",placeTime);
        map.put("orderOther4",orderOther4);
        map.put("zLong",zLong);
        return "wlcGetOrderForm_off_the_stocks";
    }

    //展示已完成刷新页页
    @RequestMapping("/wlcGetOrderForm_off_the_stocksLoad")
    public String wlcGetOrderForm_off_the_stocksLoad(Integer pageNo,Integer pageSize,Map map,String orderId,String placeTime,Integer orderOther4){
        //判断pageNo是否为空
        if(pageNo == null || pageNo < 1 ){
            pageNo = 1;
        }
        //判断pageSize是否为空
        if(pageSize == null ){
            pageSize = 10;
        }
        //初始化pageHelper
        PageHelper.startPage(pageNo,pageSize);
        //查询订单信息
        List<OrderOrder> list = orderOrderBackstageService.getOrderOrderListByOrderOther4(orderId,placeTime,orderOther4);
        //创建pageinfo对象
        PageInfo<OrderOrder> pageInfo = new PageInfo<OrderOrder>(list);
        //查询全部快递信息
        List<OrderLogisticstype> logisticstypeList = orderLogisticstypeService.getOrderLogisticstypeAllList();
        //判断总页数是否小于1
        if(pageInfo.getPages()<1){
            pageInfo.setPages(1);
        }
        //查询所有的状态
        List<OrderOrdertypeManage> ordertypeManageList = orderOrdertypeManageService.getOrderOrdertypeManageAllList();
        map.put("ordertypeManageList",ordertypeManageList);
        //将pageinfo对象存到map传递到前端
        map.put("pageInfo",pageInfo);
        //将快递信息存到map传递到前端
        map.put("logisticstypeList",logisticstypeList);
        //将条件存到map传递到前端
        map.put("orderId",orderId);
        //将条件存到map传递到前端
        map.put("placeTime",placeTime);
        map.put("orderOther4",orderOther4);
        return "wlcGetOrderForm_off_the_stocks::div1";
    }


    //根据id进行删除
    @RequestMapping("/wlcDodeleteOrderOrder")
    @ResponseBody
    public Object wlcDodeleteOrderOrder(String orderId){
        Boolean flag = false;
        Integer integer = orderOrderBackstageService.deleteOrderOrderByOrderId(orderId);
        if(integer!=null){
            flag = true;
        }
        return flag;
    }




    @RequestMapping("/index")
    public String index(){
        return "index";
    }
}
